package com.danchuang.consumer.config;

import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author haitao
 * @date 2021/7/26 11:04 上午
 */
@Configuration
public class RabbitConfig {

    public static final String QUEUE_1 = "queue-1";

    public static final String EXCHANGE_1 = "exchange-1";

    public static final String ROUTING_KEY_1 = "springboot.*";


    public static final String BASIC_QUEUE = "basic_queue";

    public static final String BASIC_EXCHANGE = "basic_exchange";

    public static final String BASIC_ROUTING = "basic_routing";

    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }


    @Bean("batchQueueRabbitListenerContainerFactory")
    public SimpleRabbitListenerContainerFactory batchQueueRabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        //设置批量
        factory.setBatchListener(true);
        //设置BatchMessageListener生效
        factory.setConsumerBatchEnabled(true);
        //设置监听器一次批量处理的消息数量
        factory.setBatchSize(10);
        return factory;
    }
}
